<template>
  <div class="article-edit">
    <el-card class="!border-none" shadow="never">
      <el-page-header :content="$route.meta.title" @back="$router.back()"/>
    </el-card>
    <el-card class="mt-4 !border-none" shadow="never" v-loading="loading">
      <el-form
          ref="formRef"
          class="ls-form"
          :model="formData"
          label-width="85px"
          :rules="rules"
      >
<!--        <div class="xl:flex">-->
<!--          <div>-->
            <el-form-item label="试题标题" prop="title">
                <el-input
                    v-model="formData.title"
                    placeholder="请输入试题标题"
                    maxlength="64"
                    show-word-limit
                    clearable
                />
            </el-form-item>
            <el-form-item label="试题栏目" prop="cid">
              <el-select
                  class="w-80"
                  v-model="formData.cid"
                  placeholder="请选择试题栏目"
                  clearable
              >
                <el-option
                    v-for="item in optionsData.article_cate"
                    :key="item.id"
                    :label="item.name + (item.type === 1 ? '(机考)' : '(纸笔)')"
                    :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="试题简介" prop="desc">
                <el-input
                    v-model="formData.desc"
                    placeholder="请输入试题简介"
                    type="textarea"
                    :autosize="{ minRows: 2, maxRows: 6 }"
                    :maxlength="200"
                    show-word-limit
                    clearable
                />
            </el-form-item>
            <el-form-item label="摘要" prop="abstract">
                <el-input
                    type="textarea"
                    :autosize="{ minRows: 2, maxRows: 6 }"
                    v-model="formData.abstract"
                    maxlength="200"
                    show-word-limit
                    clearable
                />
            </el-form-item>
            <el-form-item label="试题封面" prop="image">
              <div>
                <div>
                  <material-picker v-model="formData.image" :limit="1"/>
                </div>
                <div class="form-tips">建议尺寸：240*180px</div>
              </div>
            </el-form-item>
            <el-form-item label="试卷文件" prop="image">
              <div>
                <div>
                  <material-picker v-model="formData.pdf" :limit="1" type="file"/>
                </div>
              </div>
            </el-form-item>
            <el-form-item label="听力文件" prop="image">
              <div>
                <div>
                  <material-picker v-model="formData.listen" :limit="1" type="file"/>
                </div>
                <div class="form-tips">建议尺寸：240*180px</div>
              </div>
            </el-form-item>
        <div class="xl:flex">
          <div>
            <el-form-item label="作者" prop="author" class="w-[400px]">
              <div class="w-80">
                <el-input v-model="formData.author" placeholder="请输入作者名称"/>
              </div>
            </el-form-item>
          </div>
          <el-form-item label="排序" prop="sort" class="w-[400px]">
            <div>
              <el-input-number v-model="formData.sort" :min="0" :max="9999"/>
              <div class="form-tips">默认为0， 数值越大越排前</div>
            </div>
          </el-form-item>
        </div>
        <div class="xl:flex">
          <el-form-item label="初始浏览量" prop="click_virtual" class="w-[400px]">
            <div>
              <el-input-number v-model="formData.click_virtual" :min="0"/>
            </div>
          </el-form-item>
          <el-form-item label="试题状态" required prop="is_show" class="w-[400px]">
            <el-radio-group v-model="formData.is_show">
              <el-radio :value="1">显示</el-radio>
              <el-radio :value="0">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
        </div>

<!--          </div>-->
<!--          <div class="xl:ml-20">-->
            <el-form-item label="试题内容" prop="content">
              <editor v-model="formData.content" :height="500"/>
            </el-form-item>
<!--          </div>-->
<!--        </div>-->
      </el-form>
    </el-card>
    <footer-btns>
      <el-button type="primary" @click="handleSave">保存</el-button>
    </footer-btns>
  </div>
</template>

<script lang="ts" setup name="articleListsEdit">
import type {FormInstance} from 'element-plus'

import {articleAdd, articleCateAll, articleDetail, articleEdit} from '@/api/article'
import {useDictOptions} from '@/hooks/useDictOptions'
import useMultipleTabs from '@/hooks/useMultipleTabs'

const loading = ref(false)
const route = useRoute()
const router = useRouter()
const formData = reactive({
  id: '',
  title: '',
  image: '',
  pdf: '',
  listen: '',
  cid: '',
  desc: '',
  author: '',
  content: '',
  click_virtual: 0,
  sort: 0,
  is_show: 1,
  abstract: ''
})

const {removeTab} = useMultipleTabs()
const formRef = shallowRef<FormInstance>()
const rules = reactive({
  title: [{required: true, message: '请输入试题标题', trigger: 'blur'}],
  cid: [{required: true, message: '请选择试题栏目', trigger: 'blur'}]
})

const getDetails = async () => {
  loading.value = true;
  const data = await articleDetail({
    id: route.query.id
  })
  loading.value = false;
  Object.keys(formData).forEach((key) => {
    //@ts-ignore
    formData[key] = data[key]
  })
}

const {optionsData} = useDictOptions<{
  article_cate: any[]
}>({
  article_cate: {
    api: articleCateAll
  }
})

const handleSave = async () => {
  await formRef.value?.validate()
  loading.value = true;
  if (route.query.id) {
    await articleEdit(formData)
  } else {
    await articleAdd(formData)
  }
  loading.value = false;
  removeTab()
  router.back()
}

route.query.id && getDetails()
</script>
